package com.bootdo.system.controller;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.domain.RoleDO;
import com.bootdo.system.domain.TbOrder;
import com.bootdo.system.domain.TbUserWater;
import com.bootdo.system.domain.TbWxuser;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.OrderService;
import com.bootdo.system.service.RoleService;
import com.bootdo.system.service.UserLsService;
import com.bootdo.system.service.WxUserService;

@RequestMapping("/userLs")
@Controller
public class UserLsController {
	
	@Autowired
	private UserLsService userLsService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private WxUserService wxUserService;
	
	@Autowired
	RoleService roleService;
	
	String prefix = "system/userLs";
	
	@GetMapping("index")
	String task() {
		return prefix + "/index";
	}
	
	@ResponseBody
	@RequestMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		UserDO user = ShiroUtils.getUser();
		if(user.getUserId() != 1L) {
			List<RoleDO> list2 = roleService.getRoleByUserId(user.getUserId());
			boolean falg=false;
			for (RoleDO role : list2) {
				if(role.getRoleId() == 59L) {
					falg=true;
				}
			}
			if(falg) {//公司数据权限
				params.put("deptId", user.getDeptId());
			}else {
				params.put("userId", user.getUserId());
			}
		}
		Query query = new Query(params);
		 List<TbOrder> list = orderService.list2(query);
		List<TbOrder> res=new ArrayList<TbOrder>();
		 for (TbOrder tbOrder : list) {
			 String openid = tbOrder.getOpenid();
			TbWxuser userById = wxUserService.getUserById(openid);
			if(userById != null) {
				Integer id = userById.getId();
				tbOrder.setWxInfo(userById.getNickname()+" ( ID : "+id+" ) ");
				tbOrder.setYhye(getDouble2String2(userById.getBalance())+"");
			}
			 res.add(tbOrder);
		}
		 int count = orderService.count2(query);
		 int count3 = orderService.count3(query);
		 PageUtils pageUtil = new PageUtils(res, count+count3);
		 return pageUtil;
	}
	private Double getDouble2String2(String num) {
		if(StringUtils.isNoneBlank(num)) {
			Double cny = Double.parseDouble(num);
			DecimalFormat df = new DecimalFormat("0.00"); 
			String CNY = df.format(cny);
			Double cny1 = Double.parseDouble(CNY);
			return cny1;
		}
		return 0.00;
	}
//	@ResponseBody
//	@RequestMapping("/list")
//	public PageUtils list(@RequestParam Map<String, Object> params) {
//		Query query = new Query(params);
//		 List<TbUserWater> list = userLsService.list(query);
//		 int count = userLsService.count(query);
//		 PageUtils pageUtil = new PageUtils(list, count);
//		 return pageUtil;
//	}
}
